from datetime import datetime
from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from pydantic_validation_decorator import NotBlank

from module_admin.annotation.pydantic_annotation import as_query


class TradeModel(BaseModel):
    """
    股票交易任务表对应pydantic模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)

    id: Optional[int] = Field(default=None, description='主键ID')
    stock_code: Optional[str] = Field(default=None, description='股票代码')
    direction: Optional[str] = Field(default=None, description='交易方向：1买，2卖')
    price: Optional[Decimal] = Field(default=None, description='交易价')
    quantity: Optional[int] = Field(default=None, description='交易股数')
    source: Optional[str] = Field(default=None, description='交易来源：1手动，2策略')
    account_no: Optional[str] = Field(default=None, description='交易账户')
    phase: Optional[int] = Field(default=None, description='任务阶段')
    status: Optional[str] = Field(default=None, description='任务状态')
    create_by: Optional[str] = Field(default=None, description='创建人')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')
    update_by: Optional[str] = Field(default=None, description='更新人')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')
    remark: Optional[str] = Field(default=None, description='备注')

    @NotBlank(field_name='stock_code', message='股票代码不能为空')
    def get_stock_code(self):
        return self.stock_code

    @NotBlank(field_name='direction', message='交易方向不能为空')
    def get_direction(self):
        return self.direction

    @NotBlank(field_name='price', message='交易价不能为空')
    def get_price(self):
        return self.price

    @NotBlank(field_name='quantity', message='交易股数不能为空')
    def get_quantity(self):
        return self.quantity

    @NotBlank(field_name='source', message='交易来源不能为空')
    def get_source(self):
        return self.source

    @NotBlank(field_name='account_no', message='交易账户不能为空')
    def get_account_no(self):
        return self.account_no

    @NotBlank(field_name='status', message='任务状态不能为空')
    def get_status(self):
        return self.status

    def validate_fields(self):
        self.get_stock_code()
        self.get_direction()
        self.get_price()
        self.get_quantity()
        self.get_source()
        self.get_account_no()
        self.get_status()


class TradeQueryModel(TradeModel):
    """
    股票交易任务不分页查询模型
    """
    pass


@as_query
class TradePageQueryModel(TradeQueryModel):
    """
    股票交易任务分页查询模型
    """
    is_page: bool = Field(default=True, description='是否分页')
    page_num: int = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页记录数')


class DeleteTradeModel(BaseModel):
    """
    删除股票交易任务模型
    """

    model_config = ConfigDict(alias_generator=to_camel)

    ids: str = Field(description='需要删除的主键ID')


# 在文件末尾添加
class UpdateTradeStatusModel(BaseModel):
    """
    更新股票交易任务状态模型
    """

    model_config = ConfigDict(alias_generator=to_camel)

    id: int = Field(description='主键ID')
    status: str = Field(description='任务状态')
    update_by: Optional[str] = Field(default=None, description='更新人')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')
